<template>
  <div>
    <p style="font-size: 20px;margin-top: 100px;margin-left: 100px"><span>订单详情</span></p>
    <div v-if="order.status==null || order.status==0" style="width: 1240px">
      <el-empty description="订单已取消或不存在" :image-size="500">
      </el-empty>
    </div>
    <div v-else>
      <div class="card1">
        <div class="left">
          <p class="p1">
            <span>订单编号:</span>
            <span v-text="order.orderId"></span>
          </p>
          <div>
            <p class="p2">
              <span>订单状态:</span>
              <span v-if="activeStep==1">待付款</span>
              <span v-if="activeStep==2">待接单</span>
              <span v-if="activeStep==3">待发货</span>
              <span v-if="activeStep==4">待收货</span>
              <span v-if="activeStep==5">已收货</span>
            </p>
            <p>
              <el-button type="danger" style="margin-left: 120px;margin-top: 15px" v-if="activeStep==1"
                         @click="toCheckOut()">
                在线付款
              </el-button>
              <el-button type="success" style="margin-left: 120px;margin-top: 15px" v-if="activeStep==4"
                         @click="receive()">
                确认收货
              </el-button>
            </p>
          </div>
          <p class="step">
            <el-steps :space="150" :active="activeStep" finish-status="success" align-center>
              <el-step title="已下单"></el-step>
              <el-step title="已支付"></el-step>
              <el-step title="已接单"></el-step>
              <el-step title="已发货"></el-step>
              <el-step title="确认收货"></el-step>
            </el-steps>
          </p>
        </div>
      </div>

      <div class="card3">
        <p style="margin-bottom: 20px">
          <span class="total">详情清单</span>
        </p>
        <el-table :data="orderItems" style="width: 100%;margin-top: 10px;font-size: 20px">
          <el-table-column prop="flowerName" label="商品名称" align="center">
          </el-table-column>
          <el-table-column prop="flowerImg" label="展示图" align="center">
            <template slot-scope="scope">
              <img :src="'http://localhost:8088/image'+scope.row.flowerImg" style="width: 150px; height: 150px;"
                   alt="flowerImg">
            </template>
          </el-table-column>
          <el-table-column prop="flowerPrice" label="单价" align="center">
            <template slot-scope="scope">
              <span>￥{{ scope.row.flowerPrice }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="buyCount" label="购买数量" align="center">
          </el-table-column>
          <el-table-column prop="subTotal" label="小计" align="center">
            <template slot-scope="scope">
              <span>￥{{ scope.row.subTotal }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" v-if="activeStep==5" align="center">
            <template slot-scope="scope" v-if="order.isComment===0">
              <el-button type="success" id="commentButton" @click="getAddCommentModal(scope.row)">评论</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 添加评论的模态框 -->
      <el-dialog
          title="添加评论"
          :visible.sync="showAddCommentModal"
          width="500px">
        <el-form ref="form" :model="formData" label-width="80px">
          <el-form-item label="商品名">
            <el-input
                placeholder="orderItems.flowerName"
                v-model="formData.flowerName"
                :disabled="true">
            </el-input>
          </el-form-item>
          <el-form-item label="购买数量">
            <el-input
                placeholder="orderItems.buyCount"
                v-model="formData.buyCount"
                :disabled="true">
            </el-input>
          </el-form-item>
          <el-form-item label="评价内容">
            <el-input v-model="formData.content"></el-input>
          </el-form-item>

          <el-form-item label="评论图片">
            <el-upload
                class="avatar-uploader"
                action="http://localhost:8050/evaluate/addEvaluateImage"
                :headers="this.token"
                :show-file-list="true"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
            >
              <img v-if="formData.commentImg" :src="formData.commentImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="评分">
            <el-rate v-model="formData.rateScore"></el-rate>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitComment">提交</el-button>
            <el-button @click="showAddCommentModal = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

      <div class="card2">
        <div class="container">
          <el-descriptions class="margin-top" title="收货信息" :column="1">
            <el-descriptions-item label="收货人">{{ address.receiverName }}</el-descriptions-item>
            <el-descriptions-item label="手机号">{{ address.receiverTel }}</el-descriptions-item>
            <el-descriptions-item label="收货地址">
              {{ address.proviceName }}
              {{ address.cityName }}
              {{ address.areaName }}
              {{ address.streetName }}
              {{ address.villagesName }}
              {{ address.details }}
            </el-descriptions-item>
          </el-descriptions>
        </div>
        <div class="left" style="text-align: right;margin-right: 70px">
          <p>
            <span class="total">总金额:</span>
            <span class="total" style="margin-left: 20px;font-size: 30px;color: red">￥{{ order.total }}</span>
          </p>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import {Message} from "element-ui";

export default {
  name: "OrderItemView",
  data() {
    return {
      //步骤状态
      activeStep: 5,
      orderId: '',
      order: {},
      orderItems: [],
      address: {},
      token: {'authentication': ''},
      comments: [],
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      showAddCommentModal: false,
      formData: {
        content: '',
        flowerName: '',
        commentImg: '',
        orderId: '',
        cartId: '',
        buyCount: 0,
        rateScore: 0,
      },
      contentId: 0,
    }
  },
  created() {
    this.selectOrderByOrderId();
    this.token.authentication = sessionStorage.getItem("token") || '';
  },
  methods: {
    getAddCommentModal(data) {
      this.formData.flowerName = data.flowerName;
      this.formData.buyCount = data.buyCount;
      this.formData.contentId = data.contentId;
      this.formData.flowerId = data.flowerId;
      this.formData.orderId = data.orderId;
      this.formData.cartId = data.cartId;
      this.showAddCommentModal = true;
    },

    submitComment() {
      let accountId = sessionStorage.getItem("accountId");
      const formData = {
        content: this.formData.content,
        flowerId: this.formData.flowerId,
        accountId: accountId,
        commentImg: this.formData.commentImg,
        rateScore: this.formData.rateScore,
      }
      this.$axios.post('/evaluate/addComment', formData)
          .then(response => {
            if (response.data.code === 200) {
              Message({
                type: "success",
                message: response.data.msg,
                duration: 1000
              });
              this.contentId = response.data.data;
              this.changeToOrder();
            }
            this.showAddCommentModal = false;
            window.location.reload();
          })
          .catch(error => {
            console.error('添加评论出错:', error);
          });
    },
    changeToOrder() {
      const formData = {
        orderId: this.formData.orderId,
        cartId: this.formData.cartId,
        contentId: this.contentId
      }
      this.$axios.post('/evaluate/changeToOrder', formData).then(response => {
        if (response.data.code === 200) {
          console.log("ok")
        } else {
          console.log("error")
        }
      }).catch(
          error => {
            console.error('出错:', error);
          }
      )
    },

    handleAvatarSuccess(res) {
      this.formData.commentImg = res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },

    // 查询订单信息
    selectOrderByOrderId() {
      this.orderId = sessionStorage.getItem("orderId");
      this.$axios.get("/flowerOrder/selectOrderByOrderId",
          {
            params: {
              orderId: this.orderId,
            }
          }).then(res => {
        this.activeStep = res.data.data.status;
        this.order = res.data.data;
        this.orderItems = res.data.data.orderItems;
        console.log(this.order.isComment);
        console.log(this.order);
        this.address = res.data.data.addressVo;
      })
    },
    // 确认收货
    receive() {
      let params = new URLSearchParams;
      params.append("orderId", this.orderId);
      this.$axios.post("/flowerOrder/updateStatusByOrderId", params)
          .then(res => {
            if (res.data.code == 200) {
              Message({
                type: "success",
                message: res.data.msg,
                duration: 1000
              });
              this.selectOrderByOrderId();
            } else {
              Message({
                type: "error",
                message: res.data.msg,
                duration: 1000
              })
            }
          })
    },
    // 前往结算页面
    toCheckOut() {
      sessionStorage.setItem("orderId", this.orderId);
      this.$router.push("/checkout");
    },

  },
}
</script>

<style scoped lang="less">
.card1 {
  border-radius: 4px;
  width: 1200px;
  border: 1px solid white;
  background-color: white;
  margin-top: 20px;
  margin-left: 100px;
  height: 180px;
}

.card1 .left {
  text-align: left;
  margin-top: 50px;
}

.card1 .left .p1 {
  font-size: 13px;
  color: gray;
  margin-bottom: 10px;
  margin-left: 70px;
}

.card1 .left .p2 {
  font-size: 22px;
  margin-left: 85px;
}

.card1 .left i {
  font-size: 30px;
  vertical-align: middle;
  margin-right: 5px;
  color: #ff9100;
}

.card1 .step {
  width: 100%;
  position: absolute;
  top: 270px;
  left: 500px;
}

.card2 {
  border-radius: 4px;
  border: 1px solid white;
  background-color: white;
  margin: 30px 100px;
  width: 1200px;
}

.card2 .container {
  margin: 15px;
}

.card3 {
  border-radius: 4px;
  border: 1px solid white;
  background-color: white;
  margin: 30px 100px;
  width: 1200px;
}

.card4 {
  border-radius: 4px;
  border: 1px solid white;
  background-color: white;
  margin: 30px 100px;
}

.card4 .left {
  text-align: right;
}

.card4 .total {
  //margin-right: 200px;
  margin-left: 100px;
}

.card4 .right {
  position: absolute;
  top: 802px;
  right: 150px;
}


</style>